package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.intercepter.TokenIntercepter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Alone
 * @version 1.0
 * @date 2020/7/10 15:24
 */
@RestController
@RequestMapping("user")
public class UserLogoutController {
    /**
     * 用户登出
     * @return
     */
    @RequestMapping("loginOut")
    public ResponseData logout(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for (Cookie cookie : cookies) {
                if(TokenIntercepter.ACCESS_TOKEN.equals(cookie.getName())){
                    //销毁携带token的cookie
                    CookieUtil.enrichCookie(cookie,"/",0);
                    //覆盖原cookie
                    CookieUtil.setCookie(response,cookie);
                }
            }
        }
        return new ResponseUtil<>().setData(null);
    }
}
